$(document).ready(function () {
    if(getStorage('userRoles')=='manager'){   
        $("#basic").show();    
        $("#basic1").hide();
        ajax({
            url: "dataCount/queryData",
            data: "",
            success: function(data){
                if (data.success == true) {                   
                    userData(data.data);
                    userChartFn(data.data.dataCountList);
                } else {
                    parent.layer.msg(data.message)
                }
            }
        })
    }else{
        setTimeout(function(){
            $("#basic1").show();
            $("#basic").hide();
        },1000)
        
    }
   
    //用户数据汇总
    function userData(data){
        $("#userAllCount").text(data.totalCount.toLocaleString())
        $("#dayNewUser").text(data.dataCont[0].day.toLocaleString())
        $("#yesterdayNewUserCount").text(data.dataCont[0].yesterday.toLocaleString())
        $("#weekdayNewUserCount").text(data.dataCont[0].week.toLocaleString())
        $("#mountdayNewUserCount").text(data.dataCont[0].month.toLocaleString())
        $("#yeardayNewUserCount").text(data.dataCont[0].year.toLocaleString())
        $("#dayActiviUser").text(data.dataCont[1].day.toLocaleString())
        $("#yesterdayActiveUserCount").text(data.dataCont[1].yesterday.toLocaleString())
        $("#weekdayActiveUserCount").text(data.dataCont[1].week.toLocaleString())
        $("#mountdayActiveUserCount").text(data.dataCont[1].month.toLocaleString())
        $("#yeardayActiveUserCount").text(data.dataCont[1].year.toLocaleString())
        $("#dayOldUser").text(data.dataCont[2].day.toLocaleString())
        $("#yesterdayOldUserCount").text(data.dataCont[2].yesterday.toLocaleString())
        $("#weekdayOldUserCount").text(data.dataCont[2].week.toLocaleString())
        $("#mountdayOldUserCount").text(data.dataCont[2].month.toLocaleString())
        $("#yeardayOldUserCount").text(data.dataCont[2].year.toLocaleString())
        $('.counter').countUp({
            delay: 10,
            time: 1000
        });
    }
    function userChartFn(data){
        var userChart = echarts.init(document.getElementById('userChart'));
        var project = ["新增用户", "活跃用户", "忠实用户"]
        var date = []
        var newUserDay = []
        var activeUserDay = []
        var oldUserDay = []
        data.forEach(function(item){
            date.push(timeStamp2String(item.date, "ymd"))
            newUserDay.push(item.dataList[0])
            activeUserDay.push(item.dataList[1])
            oldUserDay.push(item.dataList[2])
        })
        userChart.setOption({
            title: {
                text: '用户数据',
                top: '10px',
                left: '20px'
            },
            tooltip: {
                trigger: 'axis'
            },
            backgroundColor: '#ffffff',
            legend: {
                data: project,
                top: '10px'
            },
            grid: {
                left: '20px',
                right: '30px',
                bottom: '4%',
                containLabel: true
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: date
            },
            yAxis: {
                type: 'value'
            },
            series: [
                {
                    name:'新增用户',
                    type:'line',
                    data:newUserDay
                },
                {
                    name:'活跃用户',
                    type:'line',
                    data:activeUserDay
                },
                {
                    name:'忠实用户',
                    type:'line',
                    data:oldUserDay
                }
            ]
        })
    }

    ajax({
        url: "dataCount/queryOrderAll",
        data: "",
        success: function(data){
            if (data.success == true) {
                orderData(data.data)
                orderChartFn(data.data.dataCountList)
            } else {
                parent.layer.msg(data.message)
            }
        }
    })
    //订单数据汇总
    function orderData(data){
        $("#orderAll").text(data.totalCount.toLocaleString())
        $("#orderDay").text(data.dataCont[0].day.toLocaleString())
        $("#orderYesterday").text(data.dataCont[0].yesterday.toLocaleString())
        $("#orderWeek").text(data.dataCont[0].week.toLocaleString())
        $("#orderMonth").text(data.dataCont[0].month.toLocaleString())
        $("#orderYear").text(data.dataCont[0].year.toLocaleString())
        $('.counter1').countUp({
            delay: 10,
            time: 1000
        });
    }
    function orderChartFn(data){
        var userChart = echarts.init(document.getElementById('orderChart'));
        var project = ["总订单数", "已支付订单", "未支付订单", "售后订单"]
        var date = []
        var all = []
        var paid = []
        var unpaid = []
        var refund = []
        data.forEach(function(item){
            date.push(timeStamp2String(item.date, "ymd"))
            all.push(item.dataList[0])
            paid.push(item.dataList[1])
            unpaid.push(item.dataList[2])
            refund.push(item.dataList[3])
        })
        userChart.setOption({
            title: {
                text: '订单数据',
                top: '10px',
                left: '20px'
            },
            tooltip: {
                trigger: 'axis'
            },
            backgroundColor: '#ffffff',
            legend: {
                data: project,
                top: '10px'
            },
            grid: {
                left: '20px',
                right: '30px',
                bottom: '4%',
                containLabel: true
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: date
            },
            yAxis: {
                type: 'value'
            },
            series: [
                {
                    name:'总订单数',
                    type:'line',
                    data:all
                },
                {
                    name:'已支付订单',
                    type:'line',
                    data:paid
                },
                {
                    name:'未支付订单',
                    type:'line',
                    data:unpaid
                },
                {
                    name:'售后订单',
                    type:'line',
                    data:refund
                }
            ]
        })
    }

    ajax({
        url: "dataCount/queryFlowData",
        data: "",
        success: function(data){
            if (data.success == true) {
                flowData(data.data)
                flowChartFn(data.data.dataCountList)
            } else {
                parent.layer.msg(data.message)
            }
        }
    })
    //流量数据汇总
    function flowData(data){
        $("#pvDay").text(data.dataCont[0].day.toLocaleString())
        $("#pvYesterday").text(data.dataCont[0].yesterday.toLocaleString())
        $("#pvWeek").text(data.dataCont[0].week.toLocaleString())
        $("#pvMonth").text(data.dataCont[0].month.toLocaleString())
        $("#pvYear").text(data.dataCont[0].year.toLocaleString())
        $("#uvDay").text(data.dataCont[1].day.toLocaleString())
        $("#uvYesterday").text(data.dataCont[1].yesterday.toLocaleString())
        $("#uvWeek").text(data.dataCont[1].week.toLocaleString())
        $("#uvMonth").text(data.dataCont[1].month.toLocaleString())
        $("#uvYear").text(data.dataCont[1].year.toLocaleString())
        $("#ipDay").text(data.dataCont[2].day.toLocaleString())
        $("#ipYesterday").text(data.dataCont[2].yesterday.toLocaleString())
        $("#ipWeek").text(data.dataCont[2].week.toLocaleString())
        $("#ipMonth").text(data.dataCont[2].month.toLocaleString())
        $("#ipYear").text(data.dataCont[2].year.toLocaleString())
        $('.data').countUp({
            delay: 10,
            time: 1000
        });
    }
    function flowChartFn(data){
        var userChart = echarts.init(document.getElementById('flowChart'));
        var project = ["浏览量（PV）", "独立访客（UV）", "独立IP（IP）"]
        var date = []
        var pv = []
        var uv = []
        var ip = []
        data.forEach(function(item){
            date.push(timeStamp2String(item.date, "ymd"))
            pv.push(item.dataList[0])
            uv.push(item.dataList[1])
            ip.push(item.dataList[2])
        })
        userChart.setOption({
            title: {
                text: '流量数据',
                top: '10px',
                left: '20px'
            },
            tooltip: {
                trigger: 'axis'
            },
            backgroundColor: '#ffffff',
            legend: {
                data: project,
                top: '10px'
            },
            grid: {
                left: '20px',
                right: '30px',
                bottom: '4%',
                containLabel: true
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: date
            },
            yAxis: {
                type: 'value'
            },
            series: [
                {
                    name:'浏览量（PV）',
                    type:'line',
                    data:pv
                },
                {
                    name:'独立访客（UV）',
                    type:'line',
                    data:uv
                },
                {
                    name:'独立IP（IP）',
                    type:'line',
                    data:ip
                }
            ]
        })
    }

    ajax({
        url: "system/dataReport/dataReportInfo",
        data: "",
        success: function(data){
            creatUser(data.data)
            creatFlow(data.data)
            creatOrder(data.data)
            creatCode(data.data)
            creatActivity(data.data)
            creatFlight(data.data)
        }
    })
    function creatUser(data){
        var tpl = "";
        for(var i=0;i<data.userDataList.length;i++){
            tpl += '<tr>' +
                '<td>'+data.userDataList[i].registerDate+'</td>' +
                '<td>'+data.userDataList[i].registerUserSum+'</td>' +
                '<td>'+data.userDataList[i].bindPhone+'</td>' +
                '<td>'+data.userDataList[i].newUserOrder+'</td>' +
                '<td>'+data.userDataList[i].orderSum+'</td>' +
                '<td>'+data.userDataList[i].conversionRate+'</td>' +
            '</tr>'
        }
        $("#userData").append(tpl)
        $("#yesNewUser").text(data.queryYesUserData.yesNewUser)
        $("#nowUserAll").text(data.queryYesUserData.nowUserAll)
        $("#nowUserPointsBaLanceAll").text(data.queryYesUserData.nowUserPointsBaLanceAll)
        $("#userAveragePointsBaLance").text(data.queryYesUserData.userAveragePointsBaLance)
    }
    function creatFlow(data){
        var tpl = "";
        for(var i=0;i<data.flowDataList.length;i++){
            tpl += '<tr>' +
                '<td>'+data.flowDataList[i].statisticsDate+'</td>' +
                '<td>'+data.flowDataList[i].globalPV+'</td>' +
                '<td>'+data.flowDataList[i].globalUV+'</td>' +
                '<td>'+data.flowDataList[i].globalIP+'</td>' +
                '<td>'+data.flowDataList[i].newVisitors+'</td>' +
                '<td>'+data.flowDataList[i].userScanPage+'</td>' +
            '</tr>'
        }
        $("#flowData").append(tpl)
        $("#new_visitors").text(data.queryYesFlowData.new_visitors)
        $("#global_pv").text(data.queryYesFlowData.global_pv)
    }
    function creatOrder(data){
        var tpl = "";
        for(var i=0;i<data.orderList.length;i++){
            tpl += '<tr>' +
                '<td>'+data.orderList[i].timer+'</td>' +
                '<td>'+data.orderList[i].orderCount+'</td>' +
                '<td>'+data.orderList[i].TotalPriceSum+'</td>' +
                '<td>'+data.orderList[i].ActualpaidAmountSum+'</td>' +
                '<td>'+data.orderList[i].IntegralOffsetAmtSum+'</td>' +
            '</tr>'
        }
        $("#orderData").append(tpl)
        $("#sumMonth").text(data.order.sumMonth)
        $("#countOrder").text(data.order.countOrder)
        $("#sumActu").text(data.order.sumActu)
    }
    function creatCode(data){
        var tpl = "";
        for(var i=0;i<data.qrcodeList.length;i++){
            tpl += '<tr>' +
                '<td>'+data.qrcodeList[i].timer+'</td>' +
                '<td>'+data.qrcodeList[i].co+'</td>' +
            '</tr>'
        }
        $("#codeData").append(tpl)
        $("#scanCount").text(data.qrcode.scanCount)
    }
    function creatFlight(data){
        var tpl = "";
        for(var i=0;i<data.flightsReminderData.length;i++){
            tpl += '<tr>' +
                '<td>'+data.flightsReminderData[i].date+'</td>' +
                '<td>'+data.flightsReminderData[i].openFlightsReminder+'</td>' +
                '<td>'+data.flightsReminderData[i].handSendReminder+'</td>' +
                '<td>'+data.flightsReminderData[i].autoSendReminder+'</td>' +
            '</tr>'
        }
        $("#flightData").append(tpl)
    }
    function creatActivity(data){
        var tpl = "";
        for(var i=0;i<data.activityList.length;i++){
            tpl += '<tr>' +
                '<td>'+data.activityList[i].timer+'</td>' +
                '<td>'+data.activityList[i].sumuv+'</td>' +
                '<td>'+data.activityList[i].sumpv+'</td>' +
                '<td>'+data.activityList[i].sumjoins+'</td>' +
                '<td>'+(data.activityList[i].sumpvuvAve*100)+'%</td>' +
            '</tr>'
        }
        $("#activityData").append(tpl)
        $("#joinCount").text(data.activity.joinCount)
    }
    $("#userDetail").on("click", function(){
        setStorage("dataType" ,"4")
    })
    $("#flowDetail").on("click", function(){
        setStorage("dataType" ,"5")
    })
    $("#orderDetail").on("click", function(){
        setStorage("dataType" ,"2")
    })
    $("#codeDetail").on("click", function(){
        setStorage("dataType" ,"3")
    })
    $("#activityDetail").on("click", function(){
        setStorage("dataType" ,"1")
    })
    $("#flightDetail").on("click", function(){
        setStorage("dataType" ,"6")
    })
});